/*
 * 用来控制list View的类
 * 在list模板中，一定要设置style, width和height
 * 例如
 * <div id="dynlist"  style="width:100%;height:100%">booklist</div>
 * 否则就出现内存已满等莫名其妙的错误 2009.09.20
 */

#include "hm_listview.h"
#include "mydatacenter.h"
#include <EIKENV.H>
#include <utf.h>

#include "mk4.h"
#include "mk4str.h"
#include "myutils.h"
 
extern MyDataCenter* iDataCenter;	
ChmListView::ChmListView()
{
	iSelected = -1;
	iControl = NULL;
}

ChmListView::~ChmListView()
{
	ClearData();
}


void ChmListView::ClearData()
{
	if(iControl == NULL)
	return;
	
	//div

}

//look book list
void ChmListView::LoadBookList()
{
	_LIT(KIdList, "dynlist"); 
	CHtmlElement* iList = iControl->Element(KIdList); 
	if(iList == NULL) return;


	iList->InvokeL(KHStrCreateList, (MListItemRenderer*)this); 

	c4_View view = iDataCenter->iDBInfo->View("booklist");
	int size = view.GetSize();

	//设置列表项模板 
	_LIT(KListItemHtml,  
	  "<div id='booklist_item' style='width:100%; height:25; border:0' focus-class='listfocus' >"
			"<div id='icon' style='width:100%; height:100%'></div>"
			"<a id='file' href=''></a>"
	  "</div>");

	iList->SetProperty(KHStrListItemHtml, KListItemHtml);

	//设置当列表为空时显示的内容 
	_LIT(KEmptyHtml, "<p align='center'>(No Book)</p>"); 
	iList->SetProperty(KHStrListEmptyHtml, KEmptyHtml); 
	
	//设置列表项目数量 
	iList->SetProperty(KHStrListItemCount, size); 
	
	//更新并重画控件
	iControl->RefreshAndDraw();		
}

void ChmListView::ParsePage(CHtmlControl* aHtmlControl)
{
	iControl = aHtmlControl;
	
	CHtmlElement* item = iControl->Element(_L("dynlist"));
	if(item == NULL) return;
	
	//
	int bufsize;
	item->GetProperty(KHStrBufSize, bufsize);
	if(bufsize > 50) return;
	
	TBuf<50> buf;
	item->GetProperty(KHStrInnerText, buf);
	if(buf == _L("booklist"))
		LoadBookList();
	
	return; 
	
}

void ChmListView::RenderListItemL(CHtmlElement* aElement, TInt aIndex)
{
	//if booklist
	CHtmlElement* icon = aElement->Element(_L("icon"));
	c4_View view = iDataCenter->iDBInfo->View("booklist");	
	TBuf<32> path;
	if(icon){
			TBuf<200> test;
			TPtrC8 content8 = DBUtil::Static()->FieldUtf8Value(&view, "bookname", aIndex);
			
			HBufC* bookname = CnvUtfConverter::ConvertToUnicodeFromUtf8L(content8);
			icon->SetProperty(KHStrInnerText, *bookname);			
			CleanupStack::PushL(bookname);
			CleanupStack::PopAndDestroy(bookname);
			
			//test.Format(_L("background-image:mem.%S"), &(data->normal_img));
			//icon->SetProperty(KHStrStyle, test);//_L("background-image:mem.logo.gif;background-position:stretch"));
			//test.Format(_L("background-image:mem.%S"), &(data->active_img));
			//icon->SetProperty(KHStrFocusStyle, test);//_L("background-image:mem.tab.gif;background-position:stretch"));			
	}
	CHtmlElement* link = aElement->Element(_L("file"));
	if(link)
		{
		const TPtrC8 temp8 = DBUtil::Static()->FieldStr8Value(&view, "filename", aIndex);
		TBuf<100> temp;	temp.Copy(temp8);
		link->SetProperty(KHStrHref, temp);
		}
}

bool ChmListView::HandleHtmlCtlEventL(const THtmlCtlEvent& aEvent)
{
	if(iControl == NULL) return false;
	
	TBuf<200> href;
	if(aEvent.iType==THtmlCtlEvent::EOnClick)
		{
		CHtmlElement* link = aEvent.iElement->ElementByTag(_L("a"));
		if(link == NULL) return false;				
		link->GetProperty(KHStrHref, href);
		//ProcessLink(href);
		CEikonEnv::Static()->InfoWinL(_L("test"), href);
		}

}
